<template>
	<view class="fix-top-window">
		<view class="uni-header">
			<uni-stat-breadcrumb class="uni-stat-breadcrumb-on-phone" />
		</view>
		<view class="uni-container">
			<!-- <uni-search-bar placeholder="自定义背景色" bgColor="#EEEEEE"  cancelButton="none" @confirm="search" /> -->
			<!-- 			<view class="uni-textarea">
				<uni-easyinput autoHeight  maxlength="2000" type="textarea" v-model="numberStr" placeholder="请输入内容"></uni-easyinput>
			</view> -->
			<uni-easyinput prefixIcon="search" v-model="numberStr" placeholder="请输入内容">
			</uni-easyinput>
			<button v-show="isShow1" style="margin-top: 20px;" type="primary" @click="startCheck">开始查重</button>
			<button v-show="isShow2" style="margin-top: 20px;" type="default" @click="submitCheck">提交审核</button>
		</view>
	</view>

	<view>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog :type="msgType" showClose=false title="结果" :content="tips">

			</uni-popup-dialog>
			<!-- <icon class="icon" type="success"></icon> -->
			<image style="display: block;top: -170px;left: 110px;width: 79px;height: 79px;" mode="scaleToFill"
				:src="src"></image>
		</uni-popup>
	</view>
	<view>
		<!-- 提示信息弹窗 -->
		<uni-popup ref="message" type="message">
			<uni-popup-message type="warn" :message="messageText" :duration="2000"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	const db = uniCloud.database()

	export default {
		data() {
			return {
				numberStr: '',
				tips: '',
				numbers: [],
				msgType: '',
				src: '',
				messageText: '',
				isShow1: true,
				isShow2: false,
				isRefreshed: false, // 防止无限刷新的标志位
			}
		},
		computed: {
			userInfo() {
				return this.$uniIdPagesStore.store.userInfo
			},
		},
		// 页面加载完成后触发（DOM 已渲染）
		onReady() {
			// 在这里执行需要页面渲染完成后的操作
			const flag = this.userInfo.role.includes("isActive") && this.userInfo.username != 'admin';
			this.isShow1 = !flag;
			this.isShow2 = flag;
		},
		methods: {
			startCheck() {
				uni.showLoading({
					title: '加载中'
				});
				console.log(this.userInfo)
				// const lines = this.numberStr.split('\n');
				// console.log(lines)
				if (this.numberStr.trim() != '') {
					console.log(this.numberStr.trim())
					const num = this.numberStr.trim();
					let that = this;
					db.collection('account')
						.where({
							number: num
						}) // 修正查询条件
						.get({
							getCount: true
						})
						.then(res => {
							console.log(res);
							uni.hideLoading();
							if (res.result.count == 0) {
								that.msgType = 'success'
								that.tips = '正常'
								that.src = '/static/成功.png'
								const usern = this.userInfo.username
								if ("admin" === usern) {
									that.saveDataByAccount(num)
								} else {
									that.saveDataByReviewed(num)
								}
							} else {
								that.msgType = 'warn'
								that.tips = '异常'
								that.src = '/static/失败.png'
							}
							that.dialogToggle(that.msgType);
						}).catch(err => {
							uni.hideLoading();
							uni.showModal({
								content: err.message || '请求服务失败',
								showCancel: false
							})
						})
				} else {
					uni.hideLoading();
					this.messageText = '请输入数据！'
					this.$refs.message.open()
				}
			},
			dialogToggle(type) {
				this.msgType = type
				this.$refs.alertDialog.open()
			},
			saveDataByReviewed(dataNum) {
				console.log(this.userInfo.username)
				console.log(dataNum)
				const currentDate = new Date();
				const dateString = currentDate.toLocaleString();
				console.log('当前日期和时间:', dateString);
				db.collection('reviewed').add({
					'account': dataNum,
					'create': this.userInfo.username,
					'createTime': dateString
				}).then(res => {
					console.log(res);
					uni.hideLoading();
				}).catch(err => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},
			saveDataByAccount(dataNum) {
				db.collection("account").add({
					'number': dataNum
				}).then(res => {

				}).catch(err => {
					uni.hideLoading();
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},
			submitCheck() {
				uni.showLoading({
					title: '加载中'
				});
				if (this.numberStr.trim() != '') {
					const num = this.numberStr.trim();
					let that = this;
					that.saveDataByReviewed(num);
					that.msgType = 'success'
					that.tips = '提交成功'
					that.src = '/static/成功.png'
					that.dialogToggle(that.msgType);
				} else {
					uni.hideLoading();
					this.messageText = '请输入数据！'
					this.$refs.message.open()
				}
			},
		}
	}
</script>

<style>
	>>>.uni-dialog-title {
		height: 100px !important;
	}
</style>